package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud.ques;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.user.YxyUserCenter;
import com.china08.yxyapi.entity.mg.user.YxyUserCenter.ResSubjectPhase;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyUserCenterRepository;

/**
 * 
 * @annotation: 云课堂--用户目录修改
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class QuesCloudLessonCatalogUpdController extends YxyApiBaseController {

    @Autowired
    private YxyUserCenterRepository yxyUserCenterRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/status/catalog/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();

        RespModel model = new RespModel();
        YxyUserCenter yxyUserCenter = yxyUserCenterRepository.findByUserId(userId);
        if (yxyUserCenter != null) {
            if (yxyUserCenter.getLastCloudLessonView() != null) {
                ResSubjectPhase resSubjectPhase = yxyUserCenter.getLastCloudLessonView();
                resSubjectPhase.setBookId(reqModel.bookId);
                resSubjectPhase.setPhaseId(reqModel.phaseId);
                resSubjectPhase.setPhaseName(reqModel.phaseName);
                resSubjectPhase.setSemester(reqModel.semesterId);
                resSubjectPhase.setSemesterName(reqModel.semesterName);
                resSubjectPhase.setSubjectId(reqModel.subjectId);
                resSubjectPhase.setSubjectName(reqModel.subjectName);
                resSubjectPhase.setVersionId(reqModel.versionId);
                resSubjectPhase.setVersionName(reqModel.versionName);
                yxyUserCenterRepository.save(yxyUserCenter);
            } else {
                yxyUserCenter.setLastCloudLessonView(reqModel.phaseId, reqModel.subjectId, reqModel.versionId,
                        reqModel.subjectName, reqModel.phaseName, reqModel.versionName, reqModel.bookId,
                        reqModel.semesterId, reqModel.semesterName);
                yxyUserCenterRepository.save(yxyUserCenter);
            }
        } else {
            YxyUserCenter userCenter = new YxyUserCenter();
            userCenter.setUserId(userId);
            userCenter.setLastCloudLessonView(reqModel.phaseId, reqModel.subjectId, reqModel.versionId,
                    reqModel.subjectName, reqModel.phaseName, reqModel.versionName, reqModel.bookId,
                    reqModel.semesterId, reqModel.semesterName);
            yxyUserCenterRepository.save(userCenter);
        }
        model.setMessage("修改成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String bookId;
        public String phaseId;
        public String phaseName;
        public String subjectId;
        public String subjectName;
        public String versionId;
        public String versionName;
        public String semesterId;
        public String semesterName;
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
